package igrupobbva.pe.sistemareniec.actions;

import igrupobbva.pe.sistemareniec.forms.PerfilForm;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.grupobbva.iici.serializable.Perfil;
import com.grupobbva.iici.service.PerfilService;
import com.grupobbva.iici.util.ConstantesUtil;
import com.grupobbva.iici.util.SpringUtils;

public class PerfilAction extends DispatchAction {
	static Logger logger = Logger.getLogger(PerfilAction.class.getName());
	private List listaUsuarios; 
	
	PerfilService perfilService;

	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); // return value
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		try {
			request.getSession().setAttribute("listaPerfil", perfilService.obtenerTodosPerfiles(flag));
			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);

	}
	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); // return value
		PerfilForm perfilForm = (PerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		Vector listMenu = null;
		String codPerfil = (String) request.getParameter("codPerfil");
		String desPerfil = (String) request.getParameter("desPerfil");
		ArrayList collecUsuario=new ArrayList();
		ArrayList listaTitulo = new ArrayList();
		
		try { 
			Perfil perfil = new Perfil();
			perfil.setCodPerfil(codPerfil);
			perfil.setDesPerfil(desPerfil);
			perfilService.modificar(perfil);
		
			request.getSession().setAttribute("listaPerfil", perfilService.obtenerTodosPerfiles(flag));			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);	

	}
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); // return value
		PerfilForm perfilForm = (PerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		Vector listMenu = null;
		String codPerfil = (String) request.getParameter("codPerfil");
		String desPerfil = (String) request.getParameter("desPerfil");
		ArrayList collecUsuario=new ArrayList();
		ArrayList listaTitulo = new ArrayList();
		
		try { 
			Perfil perfil = new Perfil();
			perfil.setCodPerfil(codPerfil);
			perfil.setDesPerfil(desPerfil);
			perfilService.registrar(perfil);		
			request.getSession().setAttribute("listaPerfil", perfilService.obtenerTodosPerfiles(flag));			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);	

	}
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward();
		PerfilForm perfilForm = (PerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		String codPerfil = (String) request.getParameter("codPerfil");
		
		try {			
			String temp[]=codPerfil.split(ConstantesUtil.SEPARADOR_CODIGOS);		
			for(int i=0;i<temp.length;i++){		
				Perfil perfil=perfilService.obtenerEntidadPorCodigo(Perfil.class, codPerfil);
				perfilService.eliminar(perfil);
			}			
			request.getSession().setAttribute("listaPerfil", perfilService.obtenerTodosPerfiles(flag));			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		return (forward);	

	}
	public ActionForward mostrarEditar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		PerfilForm perfilForm = (PerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		String codPerfil = (String) request.getParameter("codPerfil");
		try { 
			Perfil perfil = perfilService.obtenerEntidadPorCodigo(Perfil.class, codPerfil);
			perfilForm.setCodPerfil(perfil.getCodPerfil());
			perfilForm.setDesPerfil(perfil.getDesPerfil());			
			request.setAttribute("method", "editar");
			request.setAttribute("perfil", "true");
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		// Finish with
		return (forward);	

	}
	
	public ActionForward mostrarNuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		PerfilForm perfilForm = (PerfilForm) form;
		
		try { 
		
			perfilForm.setCodPerfil("");
			perfilForm.setDesPerfil("");			
			request.setAttribute("method", "nuevo");
			request.setAttribute("perfil", "false");
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		// Finish with
		return (forward);	

	}
	/**
	 * @return Returns the listaUsuarios.
	 */
	public List getListaUsuarios() {
		return listaUsuarios;
	}
	/**
	 * @param listaUsuarios The listaUsuarios to set.
	 */
	public void setListaUsuarios(List listaUsuarios) {
		this.listaUsuarios = listaUsuarios;
	}
	
	
	
	
}
